#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    ui->textEdit->setReadOnly(true);


    //客户端1
    m_tcpClientOne = new TcpClient(this);
    m_tcpClientOne->moveToThread(&m_dealTcpClientThreadOne);

    //线程销毁
    connect(&m_dealTcpClientThreadOne, &QThread::finished, this, &TcpClient::deleteLater);
    m_dealTcpClientThreadOne.start();

    //客户端2
    m_tcpClientTwo = new TcpClient(this);
    m_tcpClientTwo->moveToThread(&m_dealTcpClientThreadTwo);

    //线程销毁
    connect(&m_dealTcpClientThreadTwo, &QThread::finished, this, &TcpClient::deleteLater);
    m_dealTcpClientThreadTwo.start();


    connect(m_tcpClientOne, &TcpClient::receiveData,
            ui->textEdit, &QTextEdit::append);

    connect(m_tcpClientTwo, &TcpClient::receiveData,
            ui->textEdit_2, &QTextEdit::append);

}

MainWindow::~MainWindow()
{
    //线程销毁
    m_dealTcpClientThreadOne.quit();
    m_dealTcpClientThreadOne.wait();

    m_dealTcpClientThreadTwo.quit();
    m_dealTcpClientThreadTwo.wait();

    delete ui;
}

void MainWindow::on_pushButton_clicked()
{

}


void MainWindow::on_pushButton_3_clicked()
{
    m_tcpClientOne->sendMessage(ui->lineEdit->text().toUtf8());
}


void MainWindow::on_pushButton_4_clicked()
{
    m_tcpClientOne->sendMessage(ui->lineEdit_2->text().toUtf8());
}

